package com.wash.shoes.controller.dy.callblack;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONObject;
import com.lop.open.api.sdk.LopException;
import com.ruoyi.common.core.domain.R;
import com.wash.shoes.common.enums.OrderStatusEnum;
import com.wash.shoes.domain.dy.black.DyMsgReturnDto;
import com.wash.shoes.domain.dy.black.DyPayNotifyDto;
import com.wash.shoes.domain.dy.request.DyBookRequest;
import com.wash.shoes.domain.order.CancelOrderComponent;
import com.wash.shoes.domain.order.PayNotifyOrderComponent;
import com.wash.shoes.entity.OrderPo;
import com.wash.shoes.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lijianguo
 * @Date 25/03/2025 13:16
 * 请输入类的简介
 **/
@Api(tags = {"抖音-支付回调"})
@Log4j2
@RestController
@RequestMapping("common/pay/")
public class DyPayNotifyController {

    @Resource
    OrderService orderService;

    @Resource
    PayNotifyOrderComponent payNotifyOrderComponent;

    @Resource
    CancelOrderComponent cancelOrderComponent;

    @Resource
    DyBookRequest dyBookRequest;

    @ApiOperation("支付回调")
    @PostMapping("/notify")
    @Transactional(rollbackFor = Exception.class)
    public String notify(@RequestBody DyMsgReturnDto dyMsgReturnDto) throws LopException {
        log.info("common/pay/notify entity {}", dyMsgReturnDto);
        log.info("common/pay/notify entity {} ", JSONObject.toJSONString(dyMsgReturnDto));
        Map result=new HashMap<>();
        try {
            // 所有的请求的参数
            DyPayNotifyDto notifyDto = JSONObject.parseObject(dyMsgReturnDto.getMsg(), DyPayNotifyDto.class);
            OrderPo orderPo = orderService.getByOrderNo(notifyDto.getOut_order_no());
            log.info("notify orderPo json is {}", JSONObject.toJSONString(orderPo));
            // SUCCESS  CANCEL
            if ("SUCCESS".equals(notifyDto.getStatus())) {
                // 获取订单
                log.info("common/pay/notify SUCCESS cur status is {}", orderPo.getStatus());
                orderPo.setWxOutTradeNo(notifyDto.getOrder_id());
                orderPo.setWxTransactionId(notifyDto.getChannel_pay_id());
                orderService.updateById(orderPo);
                // 这里会有多次回调,所以只处理一次
                if (OrderStatusEnum.PRE_PAY.getStatus().equals(orderPo.getStatus())) {
                    // 抖音的预约
                    log.info("抖音的预约开始处理");
                    if (orderPo.getComeFrom().equals("3")) {
                        //TODO 这里没有传抖音的  accoutid
                        dyBookRequest.createBookPost(orderPo);
                        // 如果抖音的预约成功就开始处理物流信息
                  /*  if (orderPo.getDyBookCode().equals("0")) { TODO  by jjz 2025年9月2日 11:02:36 修改创建预约单位为异步
                        payNotifyOrderComponent.process(orderPo);
                    }*/
                    } else {
                        // 处理订单的回掉信息,快递发物流信息
                        payNotifyOrderComponent.process(orderPo);
                    }
                }
            } else {
                log.info("common/pay/notify CANCEL cur status is {}", orderPo.getStatus());
                // 如果没有取消就取消
                if (!orderPo.getStatus().equals(OrderStatusEnum.CANCEL.getStatus())) {
                    cancelOrderComponent.process(orderPo, "定时取消");
                }
            }
            result.put("err_no",0);
            result.put("err_tips","success");
        }catch (Exception e){
            result.put("err_no",400);
            result.put("err_tips","business fail");
            log.error("common/pay/notify error",e);
        }
 /*       {
            "err_no": 0,
                "err_tips": "success"
        }*/
        //固定给抖音返回这个
        return JSONUtil.toJsonStr(result);
    }

    public static void main(String[] args) {
        Map result=new HashMap<>();
        result.put("err_no",0);
        result.put("err_tips","success");
        System.out.println(JSONUtil.toJsonStr(result));
    }
}